package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.cache.Cache;
import com.stylefeng.guns.core.common.constant.cache.CacheKey;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.Ipaddress;
import com.stylefeng.guns.modular.system.service.IIpaddressService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 白名单控制器
 *
 * @author fengshuonan
 * @Date 2018-08-04 09:49:48
 */
@Controller
@RequestMapping("/ipaddress")
public class  IpaddressController extends BaseController {

    private String PREFIX = "/system/ipaddress/";

    @Autowired
    private IIpaddressService ipaddressService;

    /**
     * 跳转到白名单首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "ipaddress.html";
    }

    /**
     * 跳转到添加白名单
     */
    @RequestMapping("/ipaddress_add")
    public String ipaddressAdd() {
        return PREFIX + "ipaddress_add.html";
    }

    /**
     * 跳转到修改白名单
     */
    @RequestMapping("/ipaddress_update/{ipaddressId}")
    public String ipaddressUpdate(@PathVariable Integer ipaddressId, Model model) {
        Ipaddress ipaddress = ipaddressService.selectById(ipaddressId);
        model.addAttribute("item",ipaddress);
        LogObjectHolder.me().set(ipaddress);
        return PREFIX + "ipaddress_edit.html";
    }

    /**
     * 获取白名单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String address) {
        //组装查询
        EntityWrapper<Ipaddress> wrapper = new EntityWrapper<Ipaddress>();
        if(StringUtils.isNotBlank(address)){
            wrapper.eq("address",address);
        }
        Object object =ipaddressService.selectList(wrapper);
        return object;
    }

    /**
     * 新增白名单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增白名单" )
    @CacheEvict(value= Cache.CONSTANT, key="'" + CacheKey.IPADDRESS_IP+"'+#ipaddress.getAddress()")
    public Object add(Ipaddress ipaddress) {
        ipaddress.setOperator(ShiroKit.getUser().getName());
        ipaddressService.insert(ipaddress);

        return SUCCESS_TIP;
    }

    /**
     * 删除白名单
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除白名单")
    @CacheEvict(value= Cache.CONSTANT, key="'" + CacheKey.IPADDRESS_IP+"'+#ipaddress")
    public Object delete(@RequestParam Integer ipaddressId,@RequestParam String ipaddress) {
        ipaddressService.deleteById(ipaddressId);
        return SUCCESS_TIP;
    }



    /**
     * 白名单详情
     */
    @RequestMapping(value = "/detail/{ipaddressId}")
    @ResponseBody
    public Object detail(@PathVariable("ipaddressId") Integer ipaddressId) {
        return ipaddressService.selectById(ipaddressId);
    }
}
